package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.MaterialAppealMap;
import com.example.demo.mapper.MaterialMap;
import com.example.demo.po.Material;
import com.example.demo.po.MaterialAppeal;
import com.example.demo.po.dto.MaterialAppealParam;
import com.example.demo.service.MaterialAppealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MaterialAppealServiceImpl implements MaterialAppealService {

    @Autowired
    MaterialAppealMap materialAppealMap;

    @Autowired
    MaterialMap materialMap;

    @Override
    public Result UpdateState(MaterialAppealParam materialAppealParam) {
      int state = materialAppealParam.getState();
        System.out.println(materialAppealParam.toString());
      // 0.审批中  1.待归还   2.已归还 3.拒绝

      if (state !=0){
          //更新审核人
          materialAppealMap.UpdateAdmin(materialAppealParam);
      }
      if (state == 2){
          materialAppealMap.UpdateReturnTime(materialAppealParam);
      }


      //更新状态
        if (state == 1){

            //减去库存 //做库存判断
          Material ma =  materialMap.GetMaterialById(materialAppealParam.getMaterialId());
            System.out.println(ma.toString());
            if(ma.getNumber()< materialAppealParam.getNumber()){
                return ResultUtil.fail("申请数量超过库存！");
            }
            materialMap.DeleteNumber(materialAppealParam);
        }
        if(state == 2){
            //加上库存
            materialMap.AddNumber(materialAppealParam);
        }
        materialAppealMap.UpdateMaterialAppealState(materialAppealParam);
   return ResultUtil.success("ok");
    }

    @Override
    public Result SaveMaterialAppeal(MaterialAppeal appeal) {

//        int id ;
//
//        //借出人id
//        int rentId;
//        String rentName;
//
//        // 0.审批中  1.待归还   2.已归还 3.拒绝
//        int state ;
//
//        String borrowTime;
//        String returnTime;
//
//        int materialId;
//        String materialName;
//        int number;

        if(StringUtils.isEmpty(appeal.getRentName())||StringUtils.isEmpty(appeal.getMaterialName())){
            return ResultUtil.fail("租借人、名称必填");
        }

        if(appeal.getNumber()==0){
            return ResultUtil.fail("租借数不能为0");
        }

        Material material = materialMap.GetMaterialById(appeal.getMaterialId());
        if(appeal.getNumber()> material.getNumber()){
            return ResultUtil.fail("申请数量超过库存！");
        }


        //更新借出时间
        Date Borrowdate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        appeal.setBorrowTime(sdf.format(Borrowdate));

        appeal.setState(0);

       materialAppealMap.SaveMaterialAppeal(appeal);


         return ResultUtil.success(appeal);

    }

    @Override
    public List<MaterialAppeal> SearchMaterialAppeal(String adminName, String materialName) {
        if(!StringUtils.isEmpty(adminName)&& !StringUtils.isEmpty(materialName)){
            return     materialAppealMap.SearchMaterialAppealsByAllName("%"+adminName+"%","%"+materialName+"%");
        }else if(!StringUtils.isEmpty(adminName)){
            return materialAppealMap.SearchMaterialAppealsByadminName("%"+adminName+"%");
        }else {
            return materialAppealMap.SearchMaterialAppealsBymaterialName("%"+materialName+"%");
        }
    }

    @Override
    public Result UpdateMaterialAppeal(MaterialAppeal appeal) {
        if(StringUtils.isEmpty(appeal.getRentName())||StringUtils.isEmpty(appeal.getMaterialName())){
            return ResultUtil.fail("租借人、名称必填");
        }

        if(appeal.getNumber()==0){
            return ResultUtil.fail("租借数不能为0");
        }

        Material material = materialMap.GetMaterialById(appeal.getMaterialId());
        if(appeal.getNumber()> material.getNumber()){
            return ResultUtil.fail("申请数量超过库存！");
        }

        materialAppealMap.UpdateMaterialAppeal(appeal);

        return ResultUtil.success("更新成功");


    }


}
